package com.lottery.service.pc28.accoutService.impl;

import com.lottery.dao.pc28.accoutDao.CallCenterDao;
import com.lottery.dao.pc28.accoutDao.UserDao;
import com.lottery.dao.pc28.wealthDao.AccountDao;
import com.lottery.dao.pc28.wealthDao.AccountTransactionDao;
import com.lottery.dto.CallCenter;
import com.lottery.dto.PageDto;
import com.lottery.dto.ResultDto;
import com.lottery.entity.Account;
import com.lottery.entity.AccountTransaction;
import com.lottery.entity.RechargeInfo;
import com.lottery.entity.User;
import com.lottery.service.pc28.accoutService.CallCenterService;
import com.lottery.utils.CRMContext;
import com.lottery.utils.RandCodeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CallCenterServiceImpl implements CallCenterService {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private CallCenterDao callCenterDao;

	@Autowired
	private AccountDao accountdao;

	@Autowired
	private AccountTransactionDao accountTransactiondao;

	@Autowired
	private UserDao userDao;

	/**
	 * 模糊分页查询
	 * 
	 * @param pageNow
	 * @param callCenter
	 * @return
	 */
	@Override

	public ResultDto<Object> selectCallCenterList(String Dim, String pageNow) {

		/*
		 * ResultDto<CallCenter> result =new ResultDto<CallCenter>(); try { //获取每页的条数
		 * int pageSize=callCenter.getPageSize(); //获取起始业 int
		 * startPage=(pageNow-1)*pageSize; callCenter.setStartPage(startPage);
		 * List<CallCenter> callCenterList =
		 * callCenterDao.selectCallCenterList(callCenter);
		 * result.setList(callCenterList); result.setSuccess(true);
		 * result.setMessage("查询成功！"); } catch (Exception e) { result.setSuccess(true);
		 * result.setMessage("查询失败！"); } return result;
		 */
		ResultDto<Object> result = new ResultDto<Object>();

		try {

			final int pageSize = 10;
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("startPage", (Integer.parseInt(pageNow) - 1) * pageSize);
			map.put("pageSize", pageSize);
			map.put("Dim", Dim);
			List<CallCenter> selectCallCenterList = callCenterDao.selectCallCenterList(map);
			int pageCount = callCenterDao.getPageCount(map);
			if (pageCount == 0) {
				pageCount = 1;
			}
			PageDto pageBean = new PageDto();
			pageBean.setPageNow(Integer.parseInt(pageNow));
			pageBean.setPageSize(pageSize);
			pageBean.setTotalCount(pageCount);
			result.setList(selectCallCenterList);
			result.setData(pageBean);
			result.setMessage("200");
			result.setSuccess(true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.setMessage("400" + e);
			result.setSuccess(false);
		}
		logger.info("result信息={}", result);
		return result;

	}

	/**
	 * 修改帐户余额
	 */
	@Override
	public ResultDto<CallCenter> updateUserAccount(RechargeInfo rechargeInfo) {
		// TODO Auto-generated method stub
		ResultDto<CallCenter> resultDto = new ResultDto<CallCenter>();
		if (CRMContext.getCustomerServiceInformation() == null
				|| CRMContext.getCustomerServiceInformation().getId() == null) {
			resultDto.setMessage("1");
			resultDto.setSuccess(false);
			logger.info("客服须登陆后才能为客户充值");
			return resultDto;
		}
		Account selectAccountByUserId = accountdao.selectAccountByUserId(rechargeInfo.getUserId());
		if (null == selectAccountByUserId) {
			resultDto.setMessage("2");
			resultDto.setSuccess(false);
			return resultDto;
		}
		try {

			selectAccountByUserId.setAccountFunds(rechargeInfo.getAccountFunds());
			int updateNum = accountdao.addAccount(selectAccountByUserId);
			if (updateNum == 0) {
				resultDto.setMessage("系统异常");
				resultDto.setSuccess(false);
				return resultDto;
			}
			AccountTransaction accountTransaction = new AccountTransaction();
			accountTransaction.setAccountId(selectAccountByUserId.getId());
			accountTransaction.setTransactionMoney(rechargeInfo.getAccountFunds());

			Integer id = CRMContext.getCustomerServiceInformation().getId();
			logger.info(id.toString());
			accountTransaction.setRemarks("充值客服Id:" + id);
			accountTransaction.setMoney(rechargeInfo.getTransactionMoney());
			accountTransaction.setTransactionDate(new Date());
			accountTransaction.setMoneySources("客服充值");
			accountTransaction.setTransactionType(1);
			accountTransaction.setState(1);
			accountTransaction.setOrderId(RandCodeUtil.getOrderId());
			accountTransactiondao.insertAccountTransaction(accountTransaction);
			resultDto.setMessage("200");
			resultDto.setSuccess(true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resultDto.setMessage("400");
			resultDto.setSuccess(false);
		}
		return resultDto;

	}

	@Override
	/**
	 * 修改用户状态
	 */
	public ResultDto<CallCenter> updateUserState(CallCenter callCenter) {
		// TODO Auto-generated method stub
		ResultDto<CallCenter> resultDto = new ResultDto<CallCenter>();

		try {
			callCenterDao.updateUserState(callCenter);
			resultDto.setMessage("200");
			resultDto.setSuccess(true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resultDto.setMessage("400");
			resultDto.setSuccess(false);
		}

		return resultDto;

	}

	@Override
	public ResultDto<RechargeInfo> getRechargeScalege(RechargeInfo rechargeInfo) {
		// TODO Auto-generated method stub
		ResultDto<RechargeInfo> resultDto = new ResultDto<RechargeInfo>();
		try {
			final double rechargeScale = 1;
			double coloredDiamonds = rechargeInfo.getTransactionMoney() * rechargeScale;
			rechargeInfo.setAccountFunds(coloredDiamonds);
			resultDto.setData(rechargeInfo);
			resultDto.setSuccess(true);
			resultDto.setMessage("成功获取彩钻数量");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resultDto.setSuccess(false);
			resultDto.setMessage("未获取彩钻数量");
		}
		return resultDto;
	}

	@Override
	public ResultDto<User> getUserRealName(int userId) {
		// TODO Auto-generated method stub
		ResultDto<User> resultDto = new ResultDto<User>();
		User selectId = userDao.selectId(userId);
		if (selectId != null) {
			resultDto.setSuccess(true);
			resultDto.setMessage("用户存在");
			resultDto.setData(selectId);
		} else if (selectId == null) {
			resultDto.setSuccess(false);
			resultDto.setMessage("用户不存在");
		}
		return resultDto;
	}

	// 根据彩钻数量获取提现金额
	@Override
	public ResultDto<RechargeInfo> getWithdrawScalege(RechargeInfo rechargeInfo) {
		ResultDto<RechargeInfo> resultDto = new ResultDto<RechargeInfo>();
		try {
			final double rechargeScale = 1;
			double coloredDiamonds = rechargeInfo.getAccountFunds() * rechargeScale;
			rechargeInfo.setTransactionMoney(coloredDiamonds);
			resultDto.setData(rechargeInfo);
			resultDto.setSuccess(true);
			resultDto.setMessage("成功获取金额");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resultDto.setSuccess(false);
			resultDto.setMessage("未获取金额");
		}
		return resultDto;
	}

	// 修改保存提现结果
	@Override
	public ResultDto<CallCenter> WithdrawUpdateUserAccount(RechargeInfo rechargeInfo) {
		// TODO Auto-generated method stub
		ResultDto<CallCenter> resultDto = new ResultDto<CallCenter>();
		if (CRMContext.getCustomerServiceInformation() == null
				|| CRMContext.getCustomerServiceInformation().getId() == null) {
			resultDto.setMessage("1");
			resultDto.setSuccess(false);
			logger.info("客服须登陆后才能为客户提现");
			return resultDto;
		}

		Account selectAccountByUserId = accountdao.selectAccountByUserId(rechargeInfo.getUserId());
		if (null == selectAccountByUserId) {
			resultDto.setMessage("2");
			resultDto.setSuccess(false);
			return resultDto;
		}
		double accountFunds = selectAccountByUserId.getAccountFunds();
		double funds = rechargeInfo.getAccountFunds();
		if (accountFunds < funds) {
			resultDto.setMessage("3");
			resultDto.setSuccess(false);
			return resultDto;
		}
		try {

			selectAccountByUserId.setAccountFunds(rechargeInfo.getAccountFunds());
			int updateNum = accountdao.reduceAccount(selectAccountByUserId);
			if (updateNum == 0) {
				resultDto.setMessage("系统异常");
				resultDto.setSuccess(false);
				return resultDto;
			}

			AccountTransaction accountTransaction = new AccountTransaction();
			accountTransaction.setAccountId(selectAccountByUserId.getId());
			accountTransaction.setTransactionMoney(rechargeInfo.getAccountFunds());
			Integer id = CRMContext.getCustomerServiceInformation().getId();
			logger.info(id.toString());
			accountTransaction.setRemarks("提现客服Id:" + id);
			accountTransaction.setMoney(rechargeInfo.getTransactionMoney());
			accountTransaction.setTransactionDate(new Date());
			accountTransaction.setMoneyGo("客服提现");
			accountTransaction.setMoneySources("");
			accountTransaction.setTransactionType(2);
			accountTransaction.setOrderId(RandCodeUtil.getOrderId());
			accountTransaction.setState(1);
			accountTransactiondao.insertAccountTransaction(accountTransaction);
			resultDto.setMessage("200");
			resultDto.setSuccess(true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resultDto.setMessage("400");
			resultDto.setSuccess(false);
		}
		return resultDto;
	}

}
